package my.ezbart;

import org.xmlvm.iphone.CGRect;
import org.xmlvm.iphone.NSIndexPath;
import org.xmlvm.iphone.UIColor;
import org.xmlvm.iphone.UILabel;
import org.xmlvm.iphone.UIScreen;
import org.xmlvm.iphone.UIScrollView;
import org.xmlvm.iphone.UITableView;
import org.xmlvm.iphone.UITableViewCell;
import org.xmlvm.iphone.UITableViewController;
import org.xmlvm.iphone.UITableViewDataSource;
import org.xmlvm.iphone.UITableViewDelegate;
import org.xmlvm.iphone.UITableViewStyle;
import org.xmlvm.iphone.UIView;
import org.xmlvm.iphone.UIViewController;

/**
 * @author Cecil Holmes
 * @version 0.1
 * 
 * SelectStation.java
 * 
 * View Controller that displays all stations in a scroll-enabled table view to be selected for either
 * an origin station or a destination station.
 * 
 * Holds a Table View with cells that act as buttons that return data back to the Trip Planner.
 * Data from these views is returned to the Trip Planner and displayed in the appropriate cell.
 */
public class SelectStation extends UIViewController {
    private UIView root = new UIView(UIScreen.mainScreen().getApplicationFrame());
    private UIScrollView sv = new UIScrollView();
    private UIView newView = new UIView(new CGRect(0,0, UIScreen.mainScreen().getBounds().size.width, 
    		UIScreen.mainScreen().getBounds().size.height-110)); 
    private UITableViewController stationsController = new UITableViewController(UITableViewStyle.Plain);
    private UITableView stationsTable = stationsController.getTableView();
	private int stationType; // 0 = origin, 1 = destination
    private final static String[] stations = {
    		"12th St. Oakland City Center",
    	    "16th St. Mission",
    	    "19th St. Oakland",
    	    "24th St. Mission",
    	    "Ashby",
    	    "Balboa Park",
    	    "Bay Fair",
    	    "Castro Valley",
    	    "Civic Center/UN Plaza",
    	    "Coliseum/Oakland Airport",
    	    "Colma",
    	    "Concord",
    	    "Daly City",
    	    "Downtown Berkeley",
    	    "Dublin/Pleasanton",
    	    "El Cerrito del Norte",
    	    "El Cerrito Plaza",
    	    "Embarcadero",
    	    "Fremont",
    	    "Fruitvale",
    	    "Glen Park",
    	    "Hayward",
    	    "Lafayette",
    	    "Lake Merritt",
    	    "MacArthur",
    	    "Millbrae",
    	    "Montgomery St.",
    	    "North Berkeley",
    	    "North Concord/Martinez",
    	    "Orinda",
    	    "Pittsburg/Bay Point",
    	    "Pleasant Hill/Contra Costa Centre",
    	    "Powell St.",
    	    "Richmond",
    	    "Rockridge",
    	    "San Bruno",
    	    "San Francisco Int'l Airport",
    	    "San Leandro",
    	    "South Hayward",
    	    "South San Francisco",
    	    "Union City",
    	    "Walnut Creek",
    	    "West Dublin/Pleasanton",
    	    "West Oakland" };

    public SelectStation(int stationType) {
        /*
         * Sets the title at the top of the view
         * 
         */
    	if (stationType == 0) {
    		this.stationType = stationType;
    		this.setTitle("Select Origin");
    	} else if (stationType == 1) {
    		this.stationType = stationType;
    		this.setTitle("Select Destination");
    	}
    }
    
    /**
     * Helper function to retrieve an item from a given (section, row).
     */
    private String getItem(int row) {
        return stations[row];
    }
    
    public class TableDataSource extends UITableViewDataSource {

		@Override
		public UITableViewCell cellForRowAtIndexPath(UITableView view, NSIndexPath index) {
            UITableViewCell tc = new UITableViewCell();
            UILabel label = tc.getTextLabel();
            label.setAdjustsFontSizeToFitWidth(true);
            label.setText(stations[index.getRow()]);

            return tc;
		}

		@Override
		public int numberOfRowsInSection(UITableView view, int section) {
			return stations.length;
		}
    	
    }

    public class TableDelegate extends UITableViewDelegate {
    	
    	public void didSelectRowAtIndexPath(UITableView tableview, NSIndexPath index) {
            /**
             * This method will be called whenever the user selected a
             * particular row in the table. The specific row is given as the
             * usual (row).
             */
    		UIViewController tp = getNavigationController().getViewControllers().get(0);

        	if (stationType == 0) // origin
        		((TripPlanner) tp).setOptions(getItem(index.getRow()), stationType);
        	else if (stationType == 1) // destination
        		((TripPlanner)tp).setOptions(getItem(index.getRow()), stationType);

    		getNavigationController().popViewControllerAnimated(true);
        }
    }

    @Override
    public void loadView() {
        stationsTable.setDataSource(new TableDataSource());
        stationsTable.setDelegate(new TableDelegate());

        root.setBackgroundColor(UIColor.groupTableViewBackgroundColor);
        newView.setBackgroundColor(UIColor.groupTableViewBackgroundColor);
       
        sv.setFrame(new CGRect(0,0, UIScreen.mainScreen().getBounds().size.width, 
        		UIScreen.mainScreen().getBounds().size.height-111));
        sv.setBackgroundColor(UIColor.groupTableViewBackgroundColor);
        sv.setContentSize(newView.getBounds().size);
        sv.setScrollEnabled(true);
        
        stationsTable.setFrame(sv.getFrame());
        
        newView.addSubview(stationsTable);
        sv.addSubview(newView);
        root.addSubview(sv);

        this.setView(root);
    }
}